package top.dancun.eascourse.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.Query;

import top.dancun.eascourse.dao.CourseDao;
import top.dancun.eascourse.dao.ScoresDao;
import top.dancun.eascourse.entity.CourseEntity;
import top.dancun.eascourse.entity.ScoresEntity;
import top.dancun.eascourse.entity.StudentEntity;
import top.dancun.eascourse.feign.service.StudentFeignService;
import top.dancun.eascourse.service.ScoresService;


@Service("scoresService")
public class ScoresServiceImpl extends ServiceImpl<ScoresDao, ScoresEntity> implements ScoresService {
    @Autowired
    private CourseDao courseDao;
    @Autowired
    StudentFeignService studentFeignService;

    /**
     * 搜索成绩
     * @param pageInfo
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> pageInfo,Map<String ,String > params) {
        IPage<ScoresEntity> page = this.page(
                new Query<ScoresEntity>().getPage(pageInfo),
                Wrappers.lambdaQuery(ScoresEntity.class)
                        .eq(StringUtils.isNotBlank(params.get("courseId")),
                                ScoresEntity::getCourseId,
                                params.get("courseId"))
                        .eq(StringUtils.isNotBlank(params.get("semester")),
                                ScoresEntity:: getSemester,params.get("semester"))
                        .eq(StringUtils.isNotBlank(params.get("studentId")),
                                ScoresEntity:: getStudentId,params.get("studentId"))

        );
        //获取数据的学生id以及添加课程名称
        List<String> studentIdList = new ArrayList<>();
        page.getRecords().forEach(scores -> {
            studentIdList.add(scores.getStudentId());
            CourseEntity course =  courseDao.selectById(scores.getCourseId());
            scores.setCourse(course);
            scores.setCourseName(course.getCourseName());
        });
        //添加学生名字
        PageUtils pageUtils = studentFeignService.getByIds(new HashMap<>(),studentIdList,new ArrayList<>());
        pageUtils.getList().forEach(stu->{
            page.getRecords().forEach(scores -> {
                if(scores.getStudentId().equals(((StudentEntity)stu).getSid())){
                    scores.setStudentName(((StudentEntity) stu).getName());
                }
            });
        });

        return new PageUtils(page);
    }

    @Override
    public ScoresEntity getByTidAndCourseId(ScoresEntity scores) {
        return lambdaQuery()
                .eq(ScoresEntity::getCourseId,scores.getCourseId())
                .eq(ScoresEntity::getStudentId,scores.getStudentId())
                .eq(ScoresEntity::getSemester,scores.getSemester()).one();

    }

}